<?php

namespace App\Http\Controllers\Api\V1;

use App\Facades\Captcha; // use Captcha; 都是一样的
use App\Facades\Payment;
use App\Http\Requests\CaptchaRequest;
use App\Repositories\DemoInterface;
use App\Repositories\KeyHouseRepository;
use Dingo\Api\Exception\ResourceException;
use Gregwar\Captcha\CaptchaBuilder;
use App\Http\Controllers\Controller;

class CaptchaController extends Controller
{

    public function store(CaptchaRequest $request, CaptchaBuilder $captchaBuilder )
    {
        // facades 写法
        $result = Captcha::key();
        return $this->response->array($result)->setStatusCode(201);

        // 下面是容器的写法
        $result = resolve('App\Repositories\DemoInterface')->captcha();
        return $this->response->array($result)->setStatusCode(201);

        // 下面代码移到了 仓库下面了
        $addMinutes = config('app.captcha_addMinutes');
        $key = 'captcha-'.str_random(15);
        $captcha = $captchaBuilder->build();
        $expiredAt = now()->addMinutes($addMinutes);
        \Cache::put($key, ['code' => $captcha->getPhrase()], $expiredAt);

        $result = [
            'captcha_key' => $key,
            'expired_at' => $expiredAt->toDateTimeString(),
            'captcha_image_content' => $captcha->inline()
        ];

    }
}
